import {MapData} from "@/class/map/MapData";
import {convert_gcj02_To_Gps84, convert_gps84_To_Gcj02, MyMapOpt} from "@/class/map/myMapOpt";
import { reqDeleteMapData} from "@/api/mapdata";
import {reqPostTrack, reqPutTrack} from "@/api/track"
import { requestIsSuccess} from "@/api/request";
import {getUUID, toJson} from "@/utils/utils";
import { getColorBySpeed} from "@/utils/map_color";

export class TrackData{
    constructor({mapData, track}) {
        this.mapData=new MapData(mapData)
        this.track=new Track(track)
    }
    /**
     * 移动到该要素
     * @param map
     */
    moveTo(map){
        const element = this.getDateInMap(map)
        map.setBounds(element.getExtData().bounds)
        this.blinkData(map)
    }
    /**
     * 添加要素，并返回地图要素
     * @param map
     * @returns {*}
     */
    addToMap(map,mapCoordSys) {
        if(this.mapData.id===undefined){
            this.mapData.id=getUUID();
        }
        if(this.track.id===undefined){
            this.track.id=getUUID();
        }const result = this.getBaiduMapOpt(mapCoordSys,map)
        const bounds=(new AMap[MyMapOpt.MapType.Polyline](JSON.parse(JSON.stringify(result)))).getBounds()
        let feature = null;
        if(false) {
            const overlayGroup = []
            let lastColor = getColorBySpeed(result.speed[0].speed)
            let lastPath = []
            for (let i = 0; i < result.path.length - 1; i++) {
                let tempColor = getColorBySpeed(result.speed[i].speed)
                if(tempColor===lastColor){
                    lastPath.push(result.path[i])
                }else{
                    lastPath.push(result.path[i])
                    const overlay = new AMap[MyMapOpt.MapType.Polyline]({
                        path: lastPath,
                        strokeColor: lastColor,
                    })
                    overlayGroup.push(overlay)

                    lastColor = tempColor;
                    lastPath=[result.path[i]]
                }
            }
            feature = new AMap.OverlayGroup(overlayGroup)
            Object.assign(feature, {
                _extData: null,
                get extData() {
                    return this._extData;
                },
                set extData(value) {
                    this._extData = value;
                },
                getExtData(){
                    return this._extData;
                },
                setExtData(value){
                    this._extData = value;
                },
                remove(){
                    this.clearOverlays();
                }
            });
        }else{
            feature = new AMap[MyMapOpt.MapType.Polyline](result);
        }
        feature.setExtData({
            id: this.mapData.id,
            bounds
        })
        map.add(feature)
        return feature
    }

    /**
     * 返回地图需要的数据，如果坐标不是CJ02，转换为CJ02
     * @returns {Promise<any>}
     */
    getBaiduMapOpt(mapCoordSys,map) {
        const opt = JSON.parse(JSON.stringify(this.track))
        //坐标转换
        switch (mapCoordSys){
            case MyMapOpt.MapCoordSys.CJ02:
                if(this.mapData.coordsys!==MyMapOpt.MapCoordSys.CJ02)
                    opt.path=convert_gps84_To_Gcj02(opt.path)
                break;
            case MyMapOpt.MapCoordSys.GPS:
                if(this.mapData.coordsys!==MyMapOpt.MapCoordSys.GPS)
                    opt.path=convert_gcj02_To_Gps84(opt.path)
                break;
        }
        return opt
    }


    /**
     * 更新面数据
     * @returns true:更新成功;false:更新失败
     */
    async update() {
        const result = await reqPutTrack(this)
        return requestIsSuccess(result)
    }
    /**
     * 添加面数据
     * @returns true:更新成功;false:更新失败
     */
    async upload() {
        const result = await reqPostTrack(this)
        return requestIsSuccess(result)
    }
    /**
     * 返回要素的原始设置
     * @returns {Marker}
     */
    getElement(){
        return this.track
    }
    /**
     * 判断能否正确添加到该地图
     * @returns {boolean}
     */
    isElement(){
        return this.track.path.length>0
    }
    /**
     * 返回复制对象
     * @returns {MarkerData}
     */
    getCopy(){
        return new TrackData(this)
    }

    /**
     * 返回地图中的对象
     * @param map
     * @returns {*}
     */
    getDateInMap(map){
        return map.getAllOverlays().find(item=>{
            return item.getExtData().id===this.mapData.id
        })
    }
    /**
     * 删除map中的元素
     * @param map
     */
    deleteDataInMap(map){
        map.remove(this.getDateInMap(map))
    }
    /**
     * 闪烁该要素
     * @param map
     */
    blinkData(map){
        var mapData = this.getDateInMap(map);
        //闪烁次数
        let blinkTimes=6
        let currentTimes=0

        const interval = setInterval(()=>{
            if(currentTimes<blinkTimes){
                if(currentTimes%2===0){
                    mapData.hide()
                }else{
                    mapData.show()
                }
                currentTimes++
            }else{
                clearInterval(interval)
                mapData.show()
            }
        },100)
    }
    /**
     * 下载json数据
     */
    getJSON(){
        const context = {mapData:this.mapData,
        track:this.track}
        toJson(this.mapData.name, context)
    }
    /**
     * 删除要素
     */
    deleteElement(){
        return reqDeleteMapData(this.mapData.id)
    }
}
export class Track{
    constructor({id, path, speed,extData,speedShow}) {
        this.id=id
        this.path=path
        this.speed=speed
        this.extData=extData
        this.speedShow=speedShow
    }
}